﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class MainUI : MonoBehaviour
{
    Transform bottom, objJB;
    Slider slider;
    public int jinbi = 336;
    public Sprite FeiBiaoKong, FeibiaoShi;
	// Use this for initialization
	void Start ()
    {
        objJB = transform.Find("Right/Text");
        objJB.GetComponent<Text>().text = jinbi.ToString();
        bottom = transform.Find("Bottom");
        slider = transform.Find("Slider").GetComponent<Slider>();
        MessageCenter.instance.AddEvtListener(MsgType.ReduceFeibiao, ReduceFeiBiao);
        MessageCenter.instance.AddEvtListener(MsgType.ReduceSlider, ReduceSlider);
        MessageCenter.instance.AddEvtListener(MsgType.JinBiPlusPlus, JinbiPlusPlus);
    }
	
    void ReduceSlider(Message m)
    {
        slider.value = GameController.instance.sliderValue;
    }

    void JinbiPlusPlus(Message m)
    {
        StopAllCoroutines();
        jinbi = jinbi + 1;
        StartCoroutine(BigJB());
    }

    IEnumerator BigJB()
    {
        objJB.DOScale(1.5f, 0.15f);
        yield return new WaitForSeconds(0.15f);
        objJB.GetComponent<Text>().text = jinbi.ToString();
        objJB.DOScale(1, 0.15f);
    }

    void ReduceFeiBiao(Message m)
    {
        for (int i = 0; i < GameController.instance.lastTimes; i++)
            bottom.GetChild(i).GetComponent<Image>().overrideSprite = FeibiaoShi;
        if (GameController.instance.lastTimes < 3)
            for (int i = GameController.instance.lastTimes; i < 3; i++)
                bottom.GetChild(i).GetComponent<Image>().overrideSprite = FeiBiaoKong;
    }

    public GameObject ReturnImage()
    {
        return bottom.GetChild(GameController.instance.lastTimes - 1).gameObject;
    }

    private void OnDestroy()
    {
        MessageCenter.instance.RemoveEvtListener(MsgType.ReduceFeibiao, ReduceFeiBiao);
        MessageCenter.instance.RemoveEvtListener(MsgType.ReduceSlider, ReduceSlider);
        MessageCenter.instance.RemoveEvtListener(MsgType.JinBiPlusPlus, JinbiPlusPlus);
    }
}